package homework2;//使用德鲁伊连接池
//将德鲁伊连接池封装成一个方法


import JDBCLEARN.JdbcLearn;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

public  class HomeWorkStudentTestSystem {


    /**
     * 封装德鲁伊连接池方法
     */
    public static Connection link() throws Exception {
        //德鲁伊连接池
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/JDBCLEARN/jdbc.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);
        return connection;
    }
    /**
     * 封装预处理
     */
    public static PreparedStatement getPreparedStatement(String sql,Connection connection,Object...parameters) throws Exception {
        PreparedStatement statement = link().prepareStatement(sql);
        //1.获取元数据
        ParameterMetaData parameterMetaData = statement.getParameterMetaData();
        //2/获取字符串需要的参数个数
        int count = parameterMetaData.getParameterCount();
        //3,判断是否传参，并设置参数
        if (count != 0 && parameters != null && parameters.length == count) {
            for (int i = 0; i < count; i++) {
                statement.setObject(i + 1, parameters[i]);

            }

        }
        return statement;
    }


    /**
     * 数据的添加
     * @param sql
     * @param paremeters
     * @return
     * @throws Exception
     */
    public static int insert(String sql,Object...paremeters) throws Exception {

        //使用封装的德鲁伊连接池方法
        Connection link = HomeWorkStudentTestSystem.link();
        PreparedStatement preparedStatement = HomeWorkStudentTestSystem.getPreparedStatement(sql,link,paremeters);
        int i1 = preparedStatement.executeUpdate();
        return i1;
    }
    /**
     * 数据的查询
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static <T> T selectData(String sql,Class<T> cls,Object...paremeters) throws Exception {
            T t=null;
        //使用封装的德鲁伊连接池方法
            Connection link = HomeWorkStudentTestSystem.link();

        PreparedStatement preparedStatement = HomeWorkStudentTestSystem.getPreparedStatement(sql,link,paremeters);
            ResultSet resultSet1 = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet1.getMetaData();
                while (resultSet1.next()){
                   t =cls.newInstance();
                    int count1 = metaData.getColumnCount();
                    for (int i = 1; i < count1; i++) {
                        BeanUtils.setProperty(t,metaData.getColumnName(i),resultSet1.getObject(i));
                    }

                    }
        if (t!=null){
            System.out.println("查询成功");
        }else{
            System.out.println("查询失败");
        }
        return t;

    }

    /**
     * 删除某条记录
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static int deleteDb(String sql,Object...paremeters) throws Exception {

        //使用封装的德鲁伊连接池方法
        Connection link = HomeWorkStudentTestSystem.link();
        PreparedStatement preparedStatement = HomeWorkStudentTestSystem.getPreparedStatement(sql,link,paremeters);
        int i1 = preparedStatement.executeUpdate();

        if (i1!=0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return i1;
    }
    public static int updateData(String sql,Object...paremeters) throws Exception {
        Connection link = HomeWorkStudentTestSystem.link();
        PreparedStatement preparedStatement = HomeWorkStudentTestSystem.getPreparedStatement(sql,link,paremeters);
        int i1 = preparedStatement.executeUpdate();
        if (i1!=0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
        return i1;
    }





    public void logon() throws Exception {
        Connection connection = link();
        Student student = new Student();
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名");
        String sname = scanner.next();
        System.out.println("请输入密码");
        String ssex = scanner.next();

        String sql = "select * from student where sname=? && ssex=?";
        //4.创建数据操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,sname);
        statement.setString(2, ssex);
        //5.执行sql，获取结果集
        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
            HomeWorkStudentTestSystem homeWorkStudentTestSystem = new HomeWorkStudentTestSystem();
            homeWorkStudentTestSystem.logon();
        }


    }
    public void we(){
        // STOPSHIP: 2024/7/17

    }

}
